package com.tencent.system.controller;

import com.tencent.basic.annotation.PreAuthorize;
import com.tencent.basic.util.AjaxResult;
import com.tencent.basic.util.PageList;
import com.tencent.system.domain.Menu;
import com.tencent.system.query.MenuQuery;
import com.tencent.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    public IMenuService menuService;

    /**
     * 接口：添加或修改
     * @param menu  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @PreAuthorize(name = "菜单列表", sn = "menu:list")
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            System.out.println(menu);
            if( menu.getId()!=null) {
                menuService.update(menu);
            } else {
                menuService.save(menu);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除菜单", sn = "menu:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            menuService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除菜单", sn = "menu:patchDelete")
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
                menuService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个菜单", sn = "menu:getOne")
    public Menu findOne(@PathVariable("id")Long id) {
        return menuService.queryById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    @PreAuthorize(name = "查询所有菜单", sn = "menu:getAll")
    public List<Menu> findAll(){
        return menuService.queryAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "菜单分页权限", sn = "menu:list")
    public PageList<Menu> queryPage(@RequestBody MenuQuery query) {
        return menuService.queryPage(query);
    }

    /**
     *  接口 ： 上级树
     * http://localhost:8080/menu/menuTree + 无 + put
     */
    @GetMapping("/menuTree")
    @PreAuthorize(name = "菜单树", sn = "menu:tree")
    public List<Menu> queryMenuTree() {
        return menuService.queryDeptTree();
    }
}
